/*
    This file is part of Starlight Engine.

    Starlight Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    Starlight Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Starlight Engine.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using StarlightEngine;

namespace StarlightEngine
{
    /// <summary>
    /// Controls scenes within the engine.
    /// </summary>
    public static class SceneManager
    {
        internal static Dictionary<string, Scene> scenes;
        static string active = "Empty";
        /// <summary>
        /// Gets the active scenes key.
        /// </summary>
        /// <value>The active.</value>
        public static string Active
        {
            get { return active; }
        }

        public static Dictionary<string, Scene> Scenes
        {
            get { return scenes; }
        }

        /// <summary>
        /// Gets the active scene.
        /// </summary>
        /// <value>The active scene.</value>
        public static Scene ActiveScene
        {
            get { return scenes[active]; }           
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public static void Initialize()
        {
            scenes = new Dictionary<string, Scene>();
            scenes.Add("Empty", new Scene());
        }

        /// <summary>
        /// Adds the scene.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="scene">The scene.</param>
        public static void AddScene(string name, Scene scene)
        {
            if (!scenes.ContainsKey(name))
            {
                scene.addedAs = name;
                scenes.Add(name, scene);
                if (active == "Empty")
                    active = name;
                Logger.Write("Added Scene : Wanted Name : " + name + ", Received Name : " + name, 2);
                return;
            }
             int tmp = 1;
            bool solved = false;
            string nm = name;
            while (solved == false)
            {     
                nm = name + tmp.ToString();
                if(!scenes.ContainsKey(nm))
                    solved = true;
                else
                    tmp++;
            
            }
            scene.addedAs = nm;
            scenes.Add(nm, scene);

            //If we have no scene yet, we'll use the first added one.
            if (active == "Empty")
                active = nm;

            Logger.Write("Added Scene : Wanted Name : " + name + ", Received Name : " + nm, 2);
        }

        /// <summary>
        /// Adds the scene.
        /// </summary>
        /// <param name="scene">The scene.</param>
        public static void AddScene(Scene scene)
        {
            AddScene(scenes.Count.ToString(), scene);
        }

        /// <summary>
        /// Sets the active scene.
        /// </summary>
        /// <param name="name">The name.</param>
        public static void SetActiveScene(string name)
        {
            //TODO : Fading.
            active = name;
            Logger.Write("Set active scene to : " + name, 2);

        }
    }
}